package es.htic.iplusd.codekatas.primefactors.business.impl;

import java.util.ArrayList;
import java.util.List;

import es.htic.arq.util.ListUtils;
import es.htic.iplusd.codekatas.primefactors.business.PrimeFactorsBO;

public class PrimeFactorsBOImpl implements PrimeFactorsBO {

	public boolean isPrimeNumber(int number) {
		boolean result = true;

		List<Integer> factors	= getPrimeFactors(number);

		if (factors.size()!=1) {
			result = false;
		}

		return result;
	}

	public List<Integer> getPrimeFactors(int number) {
		ArrayList<Integer> result	= new ArrayList<Integer>();
		int candidate	= 1;
		int tempNumber	= number;

		//All numbers are divisible by 1, but 1 is NOT a Prime Number
		candidate++;

		while (candidate <= tempNumber) {
			if (tempNumber%candidate == 0) {
				tempNumber = tempNumber / candidate;
				result.add(candidate);
			} else {
				candidate++;
			}
		}

//		result = ArrayUtils.sortArray(result, ArrayUtils.SORT_ASC);

		return result;
	}

	public List<Integer> getPrimeFactors(int number, int sortedByCriteria) {
		return ListUtils.sortList(getPrimeFactors(number), sortedByCriteria);
	}

	public List<Integer> getListPrimeNumbersMinorsThan(int number) {
		ArrayList<Integer> result	= new ArrayList<Integer>();

		for (int candidate=1; candidate <= number; candidate++) {
			if (isPrimeNumber(candidate)) {
				result.add(candidate);
			}
		}

		return result;
	}
}